<?php
/**
 * Created by PhpStorm.
 * User: yuchong
 * Date: 15-5-6
 * Time: 下午4:58
 */

class Upload extends  CI_Controller{

    private $err;
    private $msg;
    private $realdir = "upload/image";
    private $dirtype = 4;
    private $maxattachsize = 6097152;
    private $upext = 'jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3';
    private $msgtype = 2;
    private $attachdir = "./upload/image/";
    private $immediate;
    private $upfile ;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     *   图片处理
     */
    public function image(){

        $this->immediate = isset($_GET['immediate'])?$_GET['immediate']:0;
        $this->upfile    = @$_FILES["filedata"];

        if(!isset($this->upfile)){
            $this->err = '文件域的name错误';
        }
        elseif(!empty($this->upfile['error']))
        {
            switch($this->upfile['error'])
            {
                case '1':
                    $this->err = '文件大小超过了php.ini定义的upload_max_filesize值';
                    break;
                case '2':
                    $this->err = '文件大小超过了HTML定义的MAX_FILE_SIZE值';
                    break;
                case '3':
                    $this->err = '文件上传不完全';
                    break;
                case '4':
                    $this->err = '无文件上传';
                    break;
                case '6':
                    $this->err = '缺少临时文件夹';
                    break;
                case '7':
                    $this->err = '写文件失败';
                    break;
                case '8':
                    $this->err = '上传被其它扩展中断';
                    break;
                case '999':
                default:
                    $this->err = '无有效错误代码';
            }
        }
        elseif(empty($this->upfile['tmp_name']) || $this->upfile['tmp_name'] == 'none')
        {
            $this->err = '无文件上传';
        }
        else
        {
            $temppath  = $this->upfile['tmp_name'];
            $fileinfo  = pathinfo($this->upfile['name']);
            $extension = $fileinfo['extension'];
            if(preg_match('/'.str_replace(',','|',$this->upext).'/i',$extension))
            {
                $bytes = filesize($temppath);
                if($bytes > $this->maxattachsize)
                {
                    $this->err = '请不要上传大小超过'.$this->formatBytes($this->maxattachsize).'的文件';
                }
                else
                {
                    switch($this->dirtype)
                    {
                        case 1: $attach_subdir = date('ymd'); break;
                        case 2: $attach_subdir = date('ym'); break;
                        case 3: $attach_subdir = 'ext_'.$extension; break;
                        case 4: $attach_subdir = date('Ym'); break;
                    }
                    $attach_dir = $this->attachdir.'/'.$attach_subdir;
                    if(!is_dir($attach_dir))
                    {
                        @mkdir($attach_dir, 0777);
                        @fclose(fopen($attach_dir.'/index.htm', 'w'));
                    }
                    PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
                    $filename = date("YmdHis").mt_rand(1000,9999).'.'.$extension;
                    $target = $attach_dir.'/'.$filename;
                    $replyPath = $this->realdir . '/' . $attach_subdir. '/' .$filename;  //url路径1
//                    @rename($this->upfile['tmp_name'],$target);
                    @move_uploaded_file($this->upfile['tmp_name'],$target);
                    $target = $this->jsonString($target);
                    if($this->immediate=='1')
                    {
                        $target = '!'.$target;
                    }
                    if($this->msgtype == 1)
                    {
                        $this->msg = "'$target'";
                    }
                    else
                    {
                        $this->msg="{'url':'http://www.5tgame.com/".$replyPath."','localname':'".$this->jsonString($this->upfile['name'])."','id':'1'}";
                    }
                }
            }else{
                $this->err = '上传文件扩展名必需为：'.$this->upext;
            }
            @unlink($temppath);
        }

        echo "{'err':'".$this->jsonString($this->err)."','msg':".$this->msg."}";
    }


    /**
     *   正则替换
     */
    public function jsonString($str)
    {
        return preg_replace("/([\\\\\/'])/",'\\\$1',$str);
    }


    /**
     *   字节转化
     */
    public function formatBytes($bytes) {
        if($bytes >= 1073741824) {
            $bytes = round($bytes / 1073741824 * 100) / 100 . 'GB';
        } elseif($bytes >= 1048576) {
            $bytes = round($bytes / 1048576 * 100) / 100 . 'MB';
        } elseif($bytes >= 1024) {
            $bytes = round($bytes / 1024 * 100) / 100 . 'KB';
        } else {
            $bytes = $bytes . 'Bytes';
        }
        return $bytes;
    }


}




?>

